package com.rayo.accesscontrollercopy.utils;

import android.content.Context;

import com.litesuits.common.utils.HexUtil;
import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.config.GlobalVariables;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by yanghaojie on 2017/3/17.
 */

public class TimeFormatUtil {
    public static String showDateTime(Context context, Date date) {
        if (date==null)
            return "null";
        String[] formats = context.getResources().getStringArray(R.array.data_format);
        String format = formats[GlobalVariables.getInstance().getmSystemData().getDateFormat()];
        StringBuilder sb = new StringBuilder(32);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        sb.append(simpleDateFormat.format(date));
        return sb.toString();
    }

    public static String dateFromYMDWHS(Context context, byte[] datetime) {
        if (datetime.length != 7) return null;
        Calendar dt = Calendar.getInstance();
        String strDt = HexUtil.encodeHexStr(datetime);
        dt.set(2000 + Integer.parseInt(strDt.substring(0, 2), 10),
                Integer.parseInt(strDt.substring(2, 4), 10) - 1,
                (byte) Integer.parseInt(strDt.substring(4, 6), 10),
                (byte) Integer.parseInt(strDt.substring(8, 10), 10),
                (byte) Integer.parseInt(strDt.substring(10, 12), 10),
                (byte) Integer.parseInt(strDt.substring(12, 14), 10));
        return showDateTime(context, dt.getTime());
    }

    public static String dateFromYMDHS(Context context, byte[] datetime) {
        if (datetime.length != 5) return null;
        String strDt = "20" + HexUtil.encodeHexStr(datetime);
        SimpleDateFormat format;
        format = new SimpleDateFormat("yyyyMMddHHmm");
        Date dt = null;
        try {
            dt = format.parse(strDt);
        } catch (Exception ex) {
            dt =  new Date();
        }
        return showDateTime(context, dt);
    }
    public static byte[] int2TwoBytes(int value) {
        byte[] src = new byte[2];
        //返回格式要求为高位在后，低位在前，所以把高位赋给第二位，低位赋给第一位
        src[0] = (byte) ((value) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        return src;
    }
    public static byte[] inttoTwoBytes(int value) {
        //高位在前
        byte[] src = new byte[2];
        src[0] = (byte) ((value>>8) & 0xFF);
        src[1] = (byte) ((value) & 0xFF);
        return src;
    }
    public static String localDateTimeFormat(Date date) {
        if (date == null)
            return "";
        return DateFormat.getDateTimeInstance().format(date);
    }
    public static Date dateFromNotYMDHM(String dt) {
        SimpleDateFormat format;
        Date result = null;

        format = new SimpleDateFormat("yyyyMMddHHmm");
        try {
            result = format.parse(dt);
        } catch (Exception ex) {
            return new Date(0);
        }
        return result;
    }
    public static String dateToTime(Date dt) {
        if (dt == null)
            return "";

        return String.format("%1$tH:%1$tM", dt);
    }
}
